package com.xique.door.device.freeview.service.biz;

import com.alibaba.fastjson.JSONObject;

import com.xique.door.util.RedisUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.device.freeview.bean.domain.FreeviewEventParam;
import com.xique.door.device.freeview.bean.domain.FreeviewEventRecord;
import com.xique.door.device.freeview.bean.dto.FreeviewUserInfoDto;
import com.xique.door.device.freeview.constant.FreeViewConstant;
import com.xique.door.device.freeview.service.IFreeviewEventParamService;
import com.xique.door.device.freeview.service.IFreeviewEventRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Auther: caogangquan
 * @Date: 2021/10/22 17:42
 * @Description:
 */
@Service
public class FreeviewEventBizService {

    @Autowired
    private IFreeviewEventParamService freeviewEventParamService;

    @Autowired
    private IFreeviewEventRecordService freeviewEventRecordService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * @description: 创建事件
     * @author caogangquan
     * @date 2021/10/22 下午7:55
     * @version 1.0
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void createEventHandle(String eventId,String param,Long eventType,String cid){
        FreeviewEventParam eventParam = new FreeviewEventParam();
        FreeviewEventRecord eventRecord = new FreeviewEventRecord();
        eventParam.setEventId(eventId);
        eventParam.setParam(param);
        redisUtil.set(FreeViewConstant.REDIS_KEY_EVENT_PREFIEX + eventId ,param,2 * 60 * 60);

        eventRecord.setEventId(eventId);
        eventRecord.setEventType(eventType);
        eventRecord.setCid(cid);
        eventRecord.setOperator(String.valueOf(SecurityUtils.getUserId()));
        eventRecord.setStatus(FreeViewConstant.EventStatus.DOING);
        freeviewEventParamService.insertFreeviewEventParam(eventParam);
        freeviewEventRecordService.insertFreeviewEventRecord(eventRecord);
    }


    /**
     * @description: 事件处理成功
     * @author caogangquan
     * @date 2021/10/22 下午7:55
     * @version 1.0
     */
    @Transactional
    public void successEventHandle(String eventId,String param,Long eventType){
        FreeviewEventRecord search = new FreeviewEventRecord();
        search.setEventId(eventId);
        FreeviewEventRecord freeviewEventRecord = freeviewEventRecordService.selectFreeviewEventRecordList(search).get(0);
        freeviewEventRecord.setStatus(FreeViewConstant.EventStatus.SUCCESS);
        freeviewEventRecord.setUpdateTime(new Date());
        freeviewEventRecordService.updateFreeviewEventRecord(freeviewEventRecord);
    }



    /**
     * @description: 事件处理失败
     * @author caogangquan
     * @date 2021/10/22 下午7:55
     * @version 1.0
     */
    @Transactional
    public void errorEventHandle(String eventId,String param,Long eventType){
        //TODO  待开发---
    }

    public List<FreeviewUserInfoDto> getUserParamByEventId(String eventId){
        //查询event对应的参数 先从redis中查询
        String jsonArray = (String) redisUtil.get(FreeViewConstant.REDIS_KEY_EVENT_PREFIEX + eventId);
        if (StringUtils.isEmpty(jsonArray)){
            FreeviewEventParam eventParam = new FreeviewEventParam();
            eventParam.setEventId(eventId);
            List<FreeviewEventParam> freeviewEventParams = freeviewEventParamService.selectFreeviewEventParamList(eventParam);
            jsonArray = freeviewEventParams.get(0).getParam();
        }

        //2、jsonarray转集合
        List<FreeviewUserInfoDto> freeviewUserInfoDtos = JSONObject.parseArray(jsonArray, FreeviewUserInfoDto.class);
        return freeviewUserInfoDtos;

    }

    public <T> List<T> getCardParamByEventId(String eventId, Class<T> toClass ){
        //查询event对应的参数 先从redis中查询
        String jsonArray = (String) redisUtil.get(FreeViewConstant.REDIS_KEY_EVENT_PREFIEX + eventId);
        if (StringUtils.isEmpty(jsonArray)){
            FreeviewEventParam eventParam = new FreeviewEventParam();
            eventParam.setEventId(eventId);
            List<FreeviewEventParam> freeviewEventParams = freeviewEventParamService.selectFreeviewEventParamList(eventParam);
            if (CollectionUtils.isEmpty(freeviewEventParams)){
                return null;
            }
            jsonArray = freeviewEventParams.get(0).getParam();
        }

        //2、jsonarray转集合
        List<T> cards = JSONObject.parseArray(jsonArray, toClass);
        return cards;

    }

    public Long getEventTypeByEventId(String eventId){
        //查询event对应的参数 先从redis中查询
        FreeviewEventRecord freeviewEventRecord = new FreeviewEventRecord();
        freeviewEventRecord.setEventId(eventId);
        List<FreeviewEventRecord> freeviewEventRecords = freeviewEventRecordService.selectFreeviewEventRecordList(freeviewEventRecord);
        return freeviewEventRecords.get(0).getEventType();

    }

    public Object getEventParamByEventId(String eventId){
        FreeviewEventParam eventParam = new FreeviewEventParam();
        eventParam.setEventId(eventId);
        List<FreeviewEventParam> freeviewEventParams = freeviewEventParamService.selectFreeviewEventParamList(eventParam);
        return freeviewEventParams.get(0).getParam();

    }

    /**
     * @description: 事件处理结果持久化到数据库
     * @author caogq
     * @date 2021/11/11 13:27
     * @version 1.0
     */

    public int updateEventStatus(String eventId, Long status, String msg) {
        FreeviewEventRecord search = new FreeviewEventRecord();
        search.setEventId(eventId);
        List<FreeviewEventRecord> freeviewEventRecords = freeviewEventRecordService.selectFreeviewEventRecordList(search);
        FreeviewEventRecord freeviewEventRecord = freeviewEventRecords.get(0);

        if (!freeviewEventRecord.getLevel().equals(1)) {
            FreeviewEventRecord parentRecord = freeviewEventRecordService.selectFreeviewEventRecordById(freeviewEventRecord.getParentId());
            parentRecord.setStatus(status);
            //父事件的状态和子事件的状态保持同步
            freeviewEventRecordService.updateFreeviewEventRecord(parentRecord);
        }

        freeviewEventRecord.setStatus(status);
        freeviewEventRecord.setRemark(msg);
        return freeviewEventRecordService.updateFreeviewEventRecord(freeviewEventRecord);
    }
}
